package com.grq.mis.validate;

import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.util.FhtUtil;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

public class FileValidate {

    // 图片格式
    private static final List<String> imgTypes = Arrays.asList("jpg","jpeg","png","bmp","gif");
    // 图片最大M
    private static final int maxImgSize = 5;

    public static void checkImg(MultipartFile file) {

        // 校验图片格式
        String typeName = FhtUtil.getFileSuffix(file);
        if (imgTypes.contains(typeName) == false){
            throw new BusinessException(ResultEnum.FILE_TYPE_ERROR,"上传图片类型错误，只支持jpg,jpeg,png,bmp,gif格式");
        }

        // 校验图片大小
        long size = file.getSize();
        if (size == 0){
            throw new BusinessException(ResultEnum.FILE_SIZE_ERROR,"上传图片大小为0");
        }
        if (size > maxImgSize * 1024 * 1024){
            throw new BusinessException(ResultEnum.FILE_SIZE_ERROR,"上传图片最大为"+maxImgSize+"MB");
        }
    }



}
